MOOC Utils

Utils like auto assignment solver extension and course notice announcment notification for MOOC courses

Architecture overview

Introduction#

Layout of the Notice Reminders API: FastAPI app factory, routers, services, Tortoise persistence, and external scrapers. Auth cookies and CORS config sit at the edges.

Project structure#

The Notice Reminders project is organized as a FastAPI application under the notice-reminders package. The structure separates concerns into:

  • Application entry points for CLI and API modes
  • Core configuration, database registration, and dependency injection
  • API routers grouped by domain resources
  • Services for scraping and business logic
  • Data models managed via Tortoise ORM
  • Pydantic settings for environment-driven configuration

Core components#

  • Application Factory: The FastAPI application is created via a factory function that configures middleware, registers routers, and initializes the database.
  • Middleware: CORS is configured using environment-driven origins.
  • Dependency Injection: LRU-cached factories supply services and settings to route handlers.
  • Database Registration: Tortoise ORM is registered with configurable schema generation and SQLite path provisioning.
  • Authentication: Cookie-based JWT tokens with helpers for current user resolution and decorator-based enforcement.

Key implementation references:

  • Application factory and router registration: notice-reminders/app/api/main.py
  • CORS configuration: notice-reminders/app/api/main.py
  • Settings and environment loading: notice-reminders/app/core/config.py
  • DI factories: notice-reminders/app/core/dependencies.py
  • Database registration: notice-reminders/app/core/database.py
  • Auth helpers: notice-reminders/app/core/auth.py

Architecture overview#

The system follows a layered architecture:

  • Entry Point: A unified CLI/API entry chooses between modes.
  • Web Layer: FastAPI app factory configures middleware and routes.
  • Domain Services: Business logic for authentication, subscriptions, courses, and scraping.
  • Persistence: Tortoise ORM models and migrations.
  • External Integrations: Scraping Swayam and future NPTEL integrations.

Detailed component analysis#

Application factory pattern and routing#

  • The application factory constructs a FastAPI app, sets title and debug flags from settings, registers CORS middleware, includes all resource routers, and registers the database.
  • Routers are grouped by domain: users, auth, subscriptions, courses, announcements, notifications, and search.

Middleware configuration (CORS)#

  • CORS is configured using environment-driven origins from settings, allowing credentials and all methods/headers.

Implementation references:

  • Middleware registration: notice-reminders/app/api/main.py
  • Origins from settings: notice-reminders/app/core/config.py

Dependency injection pattern#

  • Factories decorated with caching supply services and settings to route handlers.
  • Typical chain: route handler -> service -> external client (e.g., SwayamService).

Database registration process#

  • Tortoise configuration defines connections and apps mapping models from the models package.
  • SQLite path provisioning creates directories before schema generation.
  • Schema generation is controlled by debug flag and SQLite path existence.

Authentication and authorization#

  • Token verification resolves current user from cookies.
  • Decorator enforces authentication and injects current_user into route handlers.
  • Auth endpoints manage OTP requests, verification, token refresh, and logout.

Routing architecture#

  • Users: CRUD and notification channel management with per-user authorization checks.
  • Subscriptions: Create/list/delete subscriptions linked to courses.
  • Auth: OTP lifecycle, token refresh, logout, and profile retrieval.

Data models and relationships#

  • User, Course, and Subscription define primary entities and relationships.
  • Unique constraints prevent duplicate subscriptions per user-course pair.

External service integration (swayam and NPTEL)#

  • SwayamService encapsulates scraping operations for course search and announcements.
  • NPTEL base URL is configured for potential future integration.

Dependency analysis#

  • External libraries include FastAPI, Uvicorn, Tortoise ORM, Aerich, Pydantic Settings, httpx, beautifulsoup4, PyJWT, and multipart parsing.
  • The API entry point exposes a console script for unified CLI/API invocation.

Performance considerations#

  • Caching: LRU caches for settings and services reduce repeated instantiation overhead.
  • Database: SQLite is suitable for small-scale usage; consider PostgreSQL for production scale.
  • CORS: Allow only necessary origins to minimize preflight overhead.
  • Token TTL: Short-lived access tokens with refresh tokens improve security and reduce long-lived credential exposure.
  • Scraping: Introduce rate limiting and caching for Swayam/NPTEL endpoints to avoid overloading external services.

Troubleshooting guide#

  • CORS errors: Verify allowed origins in settings match frontend origin.
  • Database initialization: Ensure SQLite path exists or enable schema generation in debug mode.
  • Authentication failures: Confirm cookie presence and token validity; check token expiry and signing secret.
  • Route access denied: Ensure current user matches resource ownership and permissions.

Conclusion#

App factory, routers, services, ORM. Keep scraping in services so routers stay HTTP-shaped.

Appendices#

Deployment considerations#

  • Environment variables: Configure settings via .env for database URL, JWT secrets, SMTP, and CORS origins.
  • Database: Use PostgreSQL in production; ensure migrations are applied via Aerich.
  • Reverse proxy: Place behind Nginx or equivalent with HTTPS termination.
  • Scaling: Stateless API pods behind a load balancer; persist sessions via Redis if needed.